/*
 * Copyright (c) 2018 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.service.impl;

import com.darren.euc.dao.NoticeDao;
import com.darren.euc.entity.Notice;
import com.darren.euc.entity.PageBean;
import com.darren.euc.repository.NoticeRepository;
import com.darren.euc.service.NoticeService;
import com.darren.euc.util.DateUtil;
import com.google.common.collect.Lists;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 在这里编写类的功能描述
 *
 * @author fangwenhao
 * @created 2018/3/11
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeDao noticeDao;

    @Autowired
    private NoticeRepository noticeRepository;

    @Override
    public PageBean queryForPageNoticeHQL(JSONObject data, Integer pageSize, int currentPage) {
        String HQL = " from Notice where 1=1 and isDeleted = 0 order by addTime desc";
        return noticeDao.queryForPageByHQL(HQL, pageSize, currentPage);
    }

    @Override
    public PageBean queryForPageNoticeHQLReleasing(List<Long> spaceIds, Integer pageSize, int currentPage) {
        String HQL = " from Notice where 1=1 and isDeleted = 0 and status = 2 ";

        if (spaceIds.size() == 1){
            HQL +=" and userAreaId = ? ";
            return noticeDao.queryForPageByHQL(HQL, pageSize, currentPage,spaceIds.get(0));
        }else {
            HQL +=" and userAreaId = ? or  userAreaId = ?";
            return noticeDao.queryForPageByHQL(HQL, pageSize, currentPage,spaceIds.get(0),spaceIds.get(0));
        }
    }

    @Override
    public Notice findById(Long id) {
        return noticeDao.findById(id);
    }

    @Override
    public Notice update(Notice notice) {
        return noticeDao.update(notice);
    }

    @Override
    public Notice editById(JSONObject data) {
        Long id = data.getLong("id");
        Long userAreaId = data.getLong("userAreaId");
        String userArea = data.getString("userArea");
        String title = data.getString("title");
        String url = data.getString("url");
        String content = data.getString("content");
        Integer isDeleted = data.getInt("isDeleted");
        String personName = data.getString("personName");
        Date beginTime = DateUtil.string2Date(data.getString("beginTime"),"yyyy-MM-dd HH:mm:ss");
        Date endTime =  DateUtil.string2Date(data.getString("endTime"),"yyyy-MM-dd HH:mm:ss");
        if(beginTime.after(endTime)){
            return null;
        }
        Notice notice = new Notice();
        if (id != null && id >0){
            notice = noticeDao.findById(id);
        }else {
            notice.setAddTime(new Date());
            notice.setPersonName(personName);
        }
        Date now = new Date();
        if (now.before(beginTime)){
            notice.setStatus(1);
        }else if (now.after(endTime)){
            notice.setStatus(3);
        }else{
            notice.setStatus(2);
        }
        notice.setUserAreaId(userAreaId);
        notice.setUserArea(userArea);
        notice.setTitle(title);
        notice.setUrl(url);
        notice.setContent(content);
        notice.setBeginTime(beginTime);
        notice.setEndTime(endTime);
        notice.setModTime(new Date());
        notice.setIsDeleted(isDeleted);
        return noticeDao.save(notice);
    }

    @Override
    public Notice deleteById(Long id) {
        Notice notice = noticeDao.findById(id);
        notice.setIsDeleted(1);
        return noticeDao.update(notice);
    }

    @Override
    public List<Notice> findByStatusIn(List<Integer> statusList) {
        return noticeDao.findByStatusIn(statusList);
    }

    @Override
    public List<Notice> findNewTop2(Long userAreaId) {
        return noticeRepository.findTop2ByIsDeletedNotAndStatusAndUserAreaIdInOrderByAddTimeDesc(1,2, Lists.newArrayList(userAreaId,0l));
    }

    @Override
    public PageBean<Notice> findNotice(List<Long> spaceIds, Integer ps, Integer pn) {
        Pageable pageable = new PageRequest(pn-1,ps);
        Page<Notice> page = noticeRepository.findByIsDeletedNotAndStatusAndUserAreaIdInOrderByAddTimeDesc(1,2, spaceIds,pageable);
        PageBean pageBean = new PageBean();
        pageBean.setList(page.getContent());
        pageBean.setPageSize(ps);
        pageBean.setCurrentPage(pn);
        pageBean.setTotalRow(Long.valueOf(page.getTotalElements()).intValue());
        pageBean.setTotalPage(page.getTotalPages());
        return pageBean;
    }

}
